package com.hc.demo.service;

import com.hc.demo.dto.CalendarResponseDto;
import com.hc.demo.dto.OrdersMapperDto;
import com.hc.demo.mapper.HospitalMapper;
import com.hc.demo.mapper.OrdersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.Callable;

@Service
public class CalendarServiceImpl implements CalendarService{

    @Autowired
    OrdersMapper ordersMapper;

    @Autowired
    HospitalMapper hospitalMapper;

    public static void main(String[] args) {
        CalendarServiceImpl calendarService=new CalendarServiceImpl();

        List<CalendarResponseDto> calendarResponseDtoList=calendarService.getCurrentCalendarList(2025,9);

        for(CalendarResponseDto c:calendarResponseDtoList){
            System.out.println(c.getYmd()+"-余号--"+c.getRemainder()+"-总号数-"+c.getRule()+"-已经预约号数-"+c.getAppoinmentcount());
        }
    }

    //获取预约日历界面的万年历数据（含余号数量）
    public List<CalendarResponseDto> listCalendarWithRemainder(Integer year,Integer month,Integer hpId){
        List<CalendarResponseDto> calendarResponseDtoList=getCurrentCalendarList(year,month);  //第一个集合

        List<CalendarResponseDto> calendarResponseDtos=getCalendarList30(hpId);  //从明天开始

        //撮合在一起，返回给前端？那个集合打底？
        for(CalendarResponseDto crd:calendarResponseDtoList){
            for(CalendarResponseDto c:calendarResponseDtos){
                if(crd.getYmd()!=null){
                    if(crd.getYmd().equals(c.getYmd())){
                        crd.setRule(c.getRule());
                        crd.setAppoinmentcount(c.getAppoinmentcount());
                        crd.setRemainder(c.getRemainder());
                    }
                }

            }
        }
        return calendarResponseDtoList;

    }

    //获取30天预约日期列表
    public List<CalendarResponseDto> getCalendarList30(Integer hpId){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();

        //做一个临时集合，作为去Orders表中查询的参数
        List<OrdersMapperDto> parameList = new ArrayList<>();
        for(int i=0;i<30;i++) {
            OrdersMapperDto dto = new OrdersMapperDto();
            calendar.add(Calendar.DATE, 1); //计算1天后的时间
            dto.setDate(sdf.format(calendar.getTime()));
            dto.setHpId(hpId);
            parameList.add(dto);
        }

        //根据parameList参数，查询30天预约日期中，每一天的已预约人数
        //查询结果CalendarResponseDto中只有两个属性：ymd、existing，还有两个属性需要填充：total、remainder
        List<CalendarResponseDto> calendarList30 = ordersMapper.listOrdersAppointmentNumber(parameList);

        //根据医院编号，获取预约规则，就能获取每天最多预约人数
        //rule:  "0,200,200,200,200,200,100"
        // rule==>[]     [0,200,200,200,200,200,100]
        String[] strArr = hospitalMapper.selectByPrimaryKey(hpId).getRule().split(",");


        //继续填充calendarList30返回值中的total、remainder属性
        for(CalendarResponseDto cd : calendarList30) {
            // if(cd!=null){
            //将预约日期转换为Calendar对象
            try {
                calendar.setTime(sdf.parse(cd.getYmd()));  //8.26
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //通过Calendar对象的get方法获取某天为星期几，就可以通过预约规则数组获取某天的总预约人数
            int total = Integer.parseInt(strArr[calendar.get(Calendar.DAY_OF_WEEK)-1]);//总号数：200

//                if(total==0){
//                    //填充total属性
//                    cd.setRule(0);  //Rule  ---是当天可以预约的总人数
//                    //填充remainder属性
//                    cd.setRemainder(0);  //getAppoinmentCount()是获取已经预约的人数
//                }else{
            //填充total属性
            cd.setRule(total);  //Rule  ---是当天可以预约的总人数
            //填充remainder属性
            cd.setRemainder(total-cd.getAppoinmentcount());  //getAppoinmentCount()是获取已经预约的人数
            //}

        }

        //  }

        return calendarList30;
    }



    //获取对应年和对应月的日历
    private List<CalendarResponseDto> getCurrentCalendarList(Integer year, Integer month){
        List<CalendarResponseDto> currentCalendar = new ArrayList<>();

        //做万年历
        boolean isRun = false;
        if((year%4==0&&year%100!=0) || (year%400==0)) {
            isRun = true;
        }
        //计算从1900-01-01到现在的天数
        int totalDays = 0;
        for(int i=1900;i<year;i++) {
            if((i%4==0&&i%100!=0) || (i%400==0)) {
                totalDays += 366;
            }else {
                totalDays += 365;
            }
        }
        int beforeDays = 0;
        int currentDays = 0;
        for(int j=1;j<=month;j++) {
            switch(j) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    currentDays = 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    currentDays = 30;
                    break;
                case 2:
                    if(isRun) {
                        currentDays = 29;
                    }else {
                        currentDays = 28;
                    }
                    break;
            }
            if(j<month) {
                beforeDays += currentDays;
            }
        }
        totalDays += beforeDays;
        int firstDayOfMonth = totalDays%7 + 1;
        if(firstDayOfMonth == 7) {
            firstDayOfMonth = 0;
        }
        for(int k=0;k<firstDayOfMonth;k++) {
            currentCalendar.add(new CalendarResponseDto());
        }
        for(int i=1;i<=currentDays;i++) {
            String m = month<10?"0"+month:month+"";
            String d = i<10?"0"+i:i+"";
            currentCalendar.add(new CalendarResponseDto(year+"-"+m+"-"+d));
        }
        return currentCalendar;
    }

}
